given filename of a multidimensional net-cdf file the GetTimeStepsFromFile function returns the number of time steps
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | filename |
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=80), | public | :: | attribute | ||||
integer, | public, | DIMENSION(NF90_MAX_VAR_DIMS) | :: | dimIDs | |||
integer(kind=short), | public | :: | dimId | ||||
integer(kind=short), | public | :: | i | ||||
integer(kind=short), | public | :: | idTime |
Id of the variable containing information on time coordinate |
|||
integer(kind=short), | public | :: | nAtts |
number of global attributes |
|||
integer(kind=short), | public | :: | nDims |
number of dimensions |
|||
integer(kind=short), | public | :: | nDimsVar |
number of dimensions of a variable |
|||
integer(kind=short), | public | :: | nVars |
number of variables |
|||
integer(kind=short), | public | :: | ncId |
NetCdf Id for the file |
|||
integer(kind=short), | public | :: | ncStatus |
error code returned by NetCDF routines |
|||
character(len=100), | public | :: | variableName |
FUNCTION GetTimeStepsFromFile & ! (filename) & ! RESULT (steps) IMPLICIT NONE !Arguments with intent(in): CHARACTER (LEN = *), INTENT(IN) :: filename !Local declarations: INTEGER (KIND = short) :: steps INTEGER (KIND = short) :: ncStatus !!error code returned by NetCDF routines INTEGER (KIND = short) :: ncId !!NetCdf Id for the file INTEGER (KIND = short) :: nDims !!number of dimensions INTEGER (KIND = short) :: nDimsVar !!number of dimensions of a variable INTEGER (KIND = short) :: nVars !!number of variables INTEGER (KIND = short) :: nAtts !!number of global attributes INTEGER (KIND = short) :: idTime !!Id of the variable containing !!information on time coordinate INTEGER (KIND = short) :: dimId !dimension id CHARACTER (LEN = 80) :: attribute CHARACTER (LEN = 100) :: variableName INTEGER (KIND = short) :: i INTEGER, DIMENSION(NF90_MAX_VAR_DIMS) :: dimIDs !------------end of declaration------------------------------------------------ !open net-cdf file with read-only access ncStatus = nf90_open (fileName, NF90_NOWRITE, ncId) IF (ncStatus /= nf90_noerr) THEN CALL Catch ('error', 'GridLib', & TRIM (nf90_strerror (ncStatus) )//': ', & code = ncIOError, argument = fileName ) ENDIF !search for "Time" dimension ncStatus = nf90_inq_dimid(ncid, "Time", dimId) IF (ncStatus == nf90_noerr) THEN !Time dimension found ncStatus = nf90_inquire_dimension (ncId, dimId, len = steps) RETURN !no need to go on END IF !search for "time" dimension ncStatus = nf90_inq_dimid(ncid, "time", dimId) IF (ncStatus /= nf90_noerr) THEN !time dimension found ncStatus = nf90_inquire_dimension (ncId, dimId, len = steps) RETURN !no need to go on END IF !If dimension has a different name, analyse variable !inquire dataset to retrieve number of dimensions, variables !and global attributes ncStatus = nf90_inquire(ncId, nDimensions = nDims, & nVariables = nVars, & nAttributes = nAtts ) CALL ncErrorHandler (ncStatus) !search for time variable DO i = 1, nVars attribute = '' ncStatus = nf90_get_att (ncId, varid = i, name = 'standard_name', & values = attribute) IF (ncStatus == nf90_noerr) THEN !standard_name is defined IF ( attribute(1:4) == 'time' ) THEN idTime = i EXIT END IF ELSE !standard_name is not defined: search for variable named 'time' !ncStatus = nf90_inq_varid (ncId, 'time', varid = i ) ncstatus = nf90_inquire_variable(ncId, varId = i, name = variableName) IF (LEN_TRIM(variableName) == 4 .AND. & variableName(1:4) == 'time' .OR. & LEN_TRIM(variableName) == 5 .AND. & variableName(1:5) == 'Times' ) THEN !variable 'time' found idTime = i EXIT END IF END IF END DO !retrieve dimensions of variable ncStatus = nf90_inquire_variable (ncId, idTime, ndims = nDimsVar) CALL ncErrorHandler (ncStatus) !retrieve dimension id of variable ncStatus = nf90_inquire_variable(ncid, idTime, dimids = dimIDs) CALL ncErrorHandler (ncStatus) !inquire time length IF (nDimsVar == 1) THEN ncStatus = nf90_inquire_dimension (ncId, dimid = dimIDs(1), len = steps) CALL ncErrorHandler (ncStatus) ELSE IF (nDimsVar == 2) THEN ncStatus = nf90_inquire_dimension (ncId, dimid = dimIDs(2), len = steps) CALL ncErrorHandler (ncStatus) END IF RETURN END FUNCTION GetTimeStepsFromFile